/**
 * author wen
 * &copy; CUNYU GROUP
 * email wjd_13@hotmail.com
 */
package com.cunyu.service;

import cn.tom.kit.StringUtil;
import com.cunyu.bean.Form;
import com.cunyu.bean.ReqData;
import com.cunyu.bean.RespData;
import com.cunyu.dao.DBQuery;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 抢单券管理
 * @author win
 * 2019/05/11-10:06
 */
@Service
public class BillsService extends BaseService{

    public RespData<List<Map<String, Object>>> billsList(Form form, ReqData reqData){
        reqData.setOrder("desc");
        reqData.setSort("u.id");
        String sql = "select u.*,(select i.name from userInfo i where i.uid = u.uid) uname " +
                " from ucoupon u where 1=1 " ;
        DBQuery query = new DBQuery( sql ) ;
        if(StringUtil.hasLength(form.getState())){
            query.and(" u.state = ? ").setParams(Integer.valueOf(form.getState())) ;
        }
        if(StringUtil.hasLength(form.getCheck())){
            int check = Integer.valueOf(form.getCheck());
            if(check == 0){
                query.and(" u.uid = 0 ");
            }else if(check == 1){
                query.and(" u.uid > 0 ") ;
            }
        }
        return page(query.getSql(),reqData,query.getParams()) ;
    }

    public int createBills(){
        String sql = "insert into ucoupon (couid, uid, money, condition,type,code) " +
                "values (0,0,0,0,0,?) " ;
        List<Object[]> list = codeRanom(10);
        int[] ints = batchUpdate(sql,list);
        return ints.length ;
    }

    private List<Object[]> codeRanom(int size) {
        List<Object[]> res = new ArrayList<>() ;
        for (int p = 0;p < size;p ++){
            Object[] v = new Object[1] ;
            v[0] = RandomStringUtils.randomAlphabetic(8) ;
            res.add(v);
        }
        return res ;
    }

}
